package org.btik.light.mobile.common.handler.adaptor;

import android.view.View;
import android.widget.CompoundButton;
import org.btik.light.mobile.common.handler.DevBinder;
import org.btik.light.mobile.meta.mo.DefaultDevMo;

import java.util.function.Consumer;

/**
 * @author lustre
 * @since 2023/5/3 13:50
 */
public class DevBinderOnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener, DevBinder {
    private final Consumer<View> on, off;

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            on.accept(buttonView);
        } else {
            off.accept(buttonView);
        }
    }

    @Override
    public void bind(DefaultDevMo devMo) {
        if (on instanceof DevBinder) {
            ((DevBinder) on).bind(devMo);
        }
        if (off instanceof DevBinder) {
            ((DevBinder) off).bind(devMo);
        }
    }

    public DevBinderOnCheckedChangeListener(Consumer<View> on, Consumer<View> off) {
        this.on = on;
        this.off = off;
    }
}
